package org.inasnet.audio;

import javax.sound.sampled.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Capture implements Runnable 
{
	final int bufSize = 16384;
	
	String errStr;
	double duration;
	//ByteArrayOutputStream out;
	
	TargetDataLine line;
    Thread thread;
    

    void start()
    {
        errStr = null;
        thread = new Thread(this);
        thread.setName( "Capture" );
        thread.start();
    }

    void stop()
    {
        thread = null;
    }
    
    private void shutDown(String message)
    {
        if ((errStr = message) != null && thread != null)
        {
            thread = null;
            System.err.println(errStr);
        }
    }

    public void run()
    {
    	while(thread != null)
    	{
        	duration = 0;
            //audioInputStream = null;
            
	        // define the required attributes for our line, 
    	    // and make sure a compatible line is supported.
        	DataLine.Info info = new DataLine.Info(TargetDataLine.class, 
            	AudioManager.format);
        	
        	if (!AudioSystem.isLineSupported(info))
        	{
            	shutDown("Line matching " + info + " not supported.");
            	return;
        	}

            // get and open the target data line for capture.

            try
	        {
    	        line = (TargetDataLine) AudioSystem.getLine(info);
        	    line.open(AudioManager.format, line.getBufferSize());
        	}
        	catch (LineUnavailableException ex)
        	{ 
        	    shutDown("Unable to open the line: " + ex);
        	    return;
        	}
        	catch (SecurityException ex)
        	{ 
            	shutDown(ex.toString());
            	return;
        	}
       		catch (Exception ex)
        	{ 
            	shutDown(ex.toString());
            	return;
        	}

            // play back the captured audio data
	        //out = new ByteArrayOutputStream();
    	    int frameSizeInBytes = AudioManager.format.getFrameSize();
        	int bufferLengthInFrames = line.getBufferSize() / 8;
        	int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
        	byte[] data = new byte[bufferLengthInBytes];
        	int numBytesRead;
        	
        	line.start();
        	
        	while(thread != null)
        	{
        		//System.out.println("reading");
        	    if( ( numBytesRead = line.read( data, 0, bufferLengthInBytes ) ) == -1 ) 
        	    	break;
        	    //AudioManager.getSessionOutputStream().sendAudioPack( data, numBytesRead );
        	    //out.write(data, 0, numBytesRead);
        	    
        	    AudioManager.getSessionOutputStream().addAudioBytes( data, numBytesRead );
        	}

            // we reached the end of the stream.  stop and close the line.
	        line.stop();
    	    line.close();
        	line = null;

        	
        	System.out.println("END capture");
   		}
   	}
} // End class Capture
